﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HYSoft.BLL;

namespace HYSoft.Web
{
    public partial class edit88 : System.Web.UI.Page
    {
        public string typename;
        public string photo;
        public int oldmac;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Context.Session["uid"] == null || Context.Session["uid"] == "")
            {
                Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
                Context.Response.Write("alert('对不起,你登录时间超时,请重新登录');top.location.href=\"index.html\";");
                Context.Response.Write("</script>");
                Context.Response.End();
            }
            oldmac = int.Parse(Request.QueryString["mac"].ToString());
            if (!IsPostBack)
            {
                chuanganqi();
                data_binding();
            }
        }

        protected void data_binding()
        {
            DataTable dt = new monitor().GetList("").Tables[0];
            if (dt.Rows.Count > 0)
            {
                DropDownList2.DataSource = dt.DefaultView;
                DropDownList2.DataTextField = "addr";
                DropDownList2.DataValueField = "index";
                DropDownList2.DataBind();

            }
            string units1 = "";
            DataTable dt3 = new user().GetList("[index]=" + Session["uid"]).Tables[0];
            units1 = dt3.Rows[0]["unit"].ToString();
            dt3.Clear();
            dt3.Dispose();
            DataTable dt12 = new user().GetList("[unit]='" + units1 + "' and [type]=0 and [intvalue2]=1 ").Tables[0];
            if (dt.Rows.Count > 0)
            {
                DropDownList12.DataSource = dt12.DefaultView;
                DropDownList12.DataTextField = "name";
                DropDownList12.DataValueField = "index";
                DropDownList12.DataBind();
                dt12.Clear();
                dt12.Dispose();
            }
        }
        public string tux;
        protected void chuanganqi()
        {
            DataTable dt = new usr2node().GetList("[mac] = " + oldmac).Tables[0];
            this.DropDownList2.SelectedValue = dt.Rows[0]["areano"].ToString();
            this.DropDownList12.SelectedValue = dt.Rows[0]["user_index"].ToString();
            this.TextBox2.Value = dt.Rows[0]["mac"].ToString();
            this.TextBox16.Value = dt.Rows[0]["simno"].ToString();
            this.TextBox17.Value = dt.Rows[0]["addr"].ToString();
            this.TextBox20.Value = dt.Rows[0]["intvalue1"].ToString();
            this.TextBox21.Value = dt.Rows[0]["intvalue2"].ToString();
            this.TextBox4.Value = dt.Rows[0]["gongdian"].ToString();
            this.TextBox14.Value = dt.Rows[0]["rate0"].ToString();
            tux = dt.Rows[0]["tux"].ToString();
            photo = dt.Rows[0]["tux"].ToString();
            dt.Clear();
            dt.Dispose();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string images = "";
            string fileNamee = FileUpload1.FileName.ToString();
            string typ3 = fileNamee.Substring(fileNamee.LastIndexOf(".") + 1);//上传文件的后缀名
            typ3 = typ3.ToLower();
            int flag1 = 0;
            int flag2 = 0;
            if (int.Parse(this.TextBox2.Value) != oldmac)
            {
                DataTable dt8 = new gprsNode().GetList("mac='" + this.TextBox2.Value + "'").Tables[0];
                if (dt8.Rows.Count > 0)
                {
                    dt8.Clear();
                    dt8.Dispose();
                    Response.Write("<script>alert('该Mac值已存在，请确认添加传感器信息正确！');history.back();</script>");
                }
                else
                    flag1 = 1;
            }
            else
                flag1 = 1;
            if (fileNamee == "")
            {
                images = photo;
                flag2 = 2;
            }
            else
            {
                if (typ3 == "gif" || typ3 == "jpg" || typ3 == "png")
                {
                    FileUpload1.SaveAs(Server.MapPath("files/") + fileNamee);
                    images = "files/" + fileNamee + "";
                    flag2 = 1;
                }
                else
                {
                    Response.Write("<script>alert('请选择合适的安装图片格式！');history.back();</script>");
                }
            }
            if (flag1 == 1 && flag2 == 1)
            {
                var gprsNodemodels = new gprsNode().GetModelList("[mac]=" + oldmac);
                foreach (var item in gprsNodemodels)
                {
                    item.areano = Convert.ToInt32(DropDownList2.SelectedValue);
                    item.mac = int.Parse(this.TextBox2.Value);
                    item.simno = this.TextBox16.Value;
                    item.addr = this.TextBox17.Value;
                    item.intvalue1 = Convert.ToInt32(TextBox20.Value);
                    item.intvalue2 = int.Parse(this.TextBox21.Value);
                    item.gongdian = TextBox4.Value;
                    item.rate0 = int.Parse(this.TextBox14.Value);
                    item.tux = images;
                    new gprsNode().Update(item);
                }
                var node_to_usermodels = new node_to_user().GetModelList("[mac]=" + oldmac);
                foreach (var item in node_to_usermodels)
                {
                    item.user_index = Convert.ToInt32(this.DropDownList12.SelectedValue);
                    item.mac = int.Parse(this.TextBox2.Value);
                    new node_to_user().Update(item);
                }
                Response.Write("<script>alert('修改传感器成功！');window.location = 'edit88.aspx?mac=" + this.TextBox2.Value + "';</script>");
            }
            if (flag1 == 1 && flag2 == 2)
            {
                var gprsNodemodels = new gprsNode().GetModelList("[mac]=" + oldmac);
                foreach (var item in gprsNodemodels)
                {
                    item.areano = Convert.ToInt32(DropDownList2.SelectedValue);
                    item.mac = int.Parse(this.TextBox2.Value);
                    item.simno = this.TextBox16.Value;
                    item.addr = this.TextBox17.Value;
                    item.intvalue1 = Convert.ToInt32(TextBox20.Value);
                    item.intvalue2 = int.Parse(this.TextBox21.Value);
                    item.gongdian = TextBox4.Value;
                    item.rate0 = int.Parse(this.TextBox14.Value);
                    new gprsNode().Update(item);
                }
                var node_to_usermodels = new node_to_user().GetModelList("[mac]=" + oldmac);
                foreach (var item in node_to_usermodels)
                {
                    item.user_index = Convert.ToInt32(this.DropDownList12.SelectedValue);
                    item.mac = int.Parse(this.TextBox2.Value);
                    new node_to_user().Update(item);
                }
                Response.Write("<script>alert('修改传感器成功！');window.location = 'edit88.aspx?mac=" + this.TextBox2.Value + "';</script>");
            }
        }
    }
}